package com.app.view.component.scene 
{
	import flash.display.SimpleButton;
	import flash.events.MouseEvent;

	import com.peachcastle.tools.Utils;
	import com.peachcastle.core.Scene;
	import org.component.Button;
	
	import com.app.view.component.common.Toolbar;
	import com.app.view.component.HallMediator;
	import com.app.view.component.hall.UserInfo;
	import com.app.view.component.hall.HallList;
	import com.peachcastle.events.HallEvent;
	import com.app.view.manager.SceneManagerMediator;
	import com.app.view.manager.DialogManagerMediator;
	
	import com.app.model.data.UserData;
	import com.app.model.data.LevelData;
	
	import com.peachcastle.tools.Assert;
	import com.peachcastle.debug.Log;
	
	public class HallScene extends Scene 
	{
		public static const NAME:String = "HallScene";
		public static const ASSEST_NAME:String = "HallView";
		
		private var _hallMediator:HallMediator = null;
		
		private var _toolbar:Toolbar = null;
		
		private var _userLevel:LevelData = null;
		private var _userData:UserData = null;
		private var _userInfo:UserInfo = null;
		private var _halllist:HallList = null;

		private var _btn_createRoom:Button = null;
		private var _btn_startGame:SimpleButton = null;
		private var _btn_joinRoom:Button = null;

		public function HallScene(swfUrlFunc:Function = null) 
		{
			super(NAME, swfUrlFunc);
		}
		
		public function setMediator(m:HallMediator):void
		{
			_hallMediator = m;
		}
		
		override protected function enterScene():void 
		{
			super.enterScene();
		}
		
		override protected function showLoading(percent:Number = 0):void 
		{
			super.showLoading(percent);
		}
		
		override protected function initializationView():void 
		{
			super.initializationView();

			super._assest = new (super.getLibByClass("hall", ASSEST_NAME));
			Assert.notNull(super._assest, 'hall');
			
			Utils.stopClip(assest);
			assest.cacheAsBitmap = true;
			
			//断言
			Assert.hasNot(assest, 'toolbar', 'hall');
			Assert.hasNot(assest, 'btn_createRoom', 'hall');
			Assert.hasNot(assest, 'btn_joinRoom', 'hall');
			Assert.hasNot(assest, 'btn_startGame', 'hall');
			Assert.hasNot(assest, 'hall_listView', 'hall');
			
			_btn_createRoom = new Button(assest["btn_createRoom"]);
			_btn_startGame 	= assest["btn_startGame"];
			_btn_joinRoom = new Button(assest["btn_joinRoom"]);
			
			_toolbar = new Toolbar(assest["toolbar"], _hallMediator);
			_userInfo = new UserInfo(assest['user_info']);
			_halllist = new HallList(assest['hall_listView']);
			
			_hallMediator.sendNotification(SceneManagerMediator.CURRENT_SCENE_REQUEST);
			_hallMediator.sendNotification(DialogManagerMediator.SET_STAGE_SIZE, [NAME, 760, 610]);
		}
		
		override protected function configureEventListeners():void 
		{
			super.configureEventListeners();

			_btn_startGame.addEventListener(MouseEvent.CLICK, _hallMediator.onStartGame);
			_btn_joinRoom.addEventListener(MouseEvent.CLICK, _hallMediator.onJoinRoom);
			
			_halllist.addEventListener(HallEvent.HALL_SELECT_ITEM, _hallMediator.onHallSelectItem);
			_halllist.addEventListener(HallEvent.HALL_SELECT_ROOM, _hallMediator.onHallSelectRoom);
		}
		
		override protected function removeEventListeners():void
		{
			super.removeEventListeners();
			
			_btn_createRoom.removeEventListener(MouseEvent.CLICK, _hallMediator.onCreateRoom);
			_btn_startGame.removeEventListener(MouseEvent.CLICK, _hallMediator.onStartGame);
			_halllist.removeEventListener(HallEvent.HALL_SELECT_ITEM, _hallMediator.onHallSelectItem);
			_halllist.removeEventListener(HallEvent.HALL_SELECT_ROOM, _hallMediator.onHallSelectRoom);
		}
		
		public function setUserData(data:UserData, userLevel:LevelData):void
		{
			_userData = data;
			_userLevel = userLevel;
		}

		override public function addLayer():void 
		{
			super.addLayer();
			
			_userInfo.update(_userData);
			_toolbar.update(_userData, false, _userLevel);
			if (_userData.chips >= 10 * 600)
			{
				setCreateRoomBtn(true);
			}else 
			{
				setCreateRoomBtn(false);	
			}
		}
		
		override protected function removeLayer():void 
		{
			super.removeLayer();
			
			if (assest.parent)
			{
				assest.parent.removeChild(assest);
			}
		}
		
		private function setCreateRoomBtn(b:Boolean):void
		{
			if (b)
			{
				_btn_createRoom.addEventListener(MouseEvent.CLICK, _hallMediator.onCreateRoom);
			}else
			{
				_btn_createRoom.disabled = true;
			}
		}
		
		public function get halllist():HallList { return _halllist; }
		
		public function get toolbar():Toolbar { return _toolbar; }
		
		public function get userData():UserData { return _userData; }
	}

}